﻿using LuShanTea.Admin.Web.App_Start;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Admin.Web.Controllers
{
    public class PackController : Controller
    {
        public IPackService PackService { get; set; }

        [CheckPermission("Pack.List")]
        // GET: Pack
        public ActionResult List()
        {
            var packs = PackService.GetAll();
            return View(packs);
        }

        [CheckPermission("Pack.Add")]
        public ActionResult AddNew()
        {
            return View();
        }

        [CheckPermission("Pack.Add")]
        [HttpPost]
        public ActionResult AddNew(string name, float price)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }

            var Pack = PackService.GetByName(name);

            if (Pack != null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = name + "已存在" });
            }

            long packId = PackService.AddNew(name, price);

            return Json(new AjaxResult { Status = "ok" });
        }

        [CheckPermission("Pack.Edit")]
        [HttpGet]
        public ActionResult Edit(long id)
        {
            var Pack = PackService.GetById(id);

            if (Pack == null)
            {
                return Content("未找到ID为" + id + "的包装");
            }

            return View(Pack);
        }

        [CheckPermission("Pack.Edit")]
        [HttpPost]
        public ActionResult Edit(long id, string name, float price)
        {
            //验证数据
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }

            //获取数据判断包装是否存在
            var Pack = PackService.GetById(id);

            if (Pack == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "未找到ID为" + id + "的包装,请检查是否已被删除！" });
            }

            //获取名称判断修改的包装是否已存在并且不为自身原有名称
            var dataPack = PackService.GetByName(name);

            if (dataPack != null && Pack.Id != dataPack.Id)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = name + "包装已存在" });
            }

            PackService.UpdatePack(id, name, price);

            return Json(new AjaxResult { Status = "ok" });
        }

        [CheckPermission("Pack.Delete")]
        [HttpPost]
        public ActionResult Delete(long packId)
        {
            PackService.MarkDeleted(packId);

            return Json(new AjaxResult { Status = "ok" });
        }


        //[HttpPost]
        //public ActionResult BatchDel(long[] packIds)
        //{
        //    foreach (var provId in packIds)
        //    {
        //        PackService.MarkDeleted(provId);
        //    }

        //    return Json(new AjaxResult { Status = "ok" });
        //}
    }
}